﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Client;

namespace Client
{
    /*interface ISoundUtil
    {
        void LoadSoundList(string flname);
        void PlaySound(int idx);
        // function    PlayBGM         (wavname: string): TDirectSoundBuffer;
        void SilenceSound();
        // void ItemClickSound(std: TStdItem);
        void ItemUseSound(int stdmode);
    }*/

    public class SoundUtil
    {
        public const string bmg_intro             = @"wav\log-in-long2.wav";
        public const string bmg_select            = @"wav\sellect-loop2.wav";
        public const string bmg_field             = @"wav\Field2.wav";
        public const string bmg_gameover          = @"wav\game-over2.wav";

        public const int s_walk_ground_l          = 1;
        public const int s_walk_ground_r          = 2;
        public const int s_run_ground_l           = 3;
        public const int s_run_ground_r           = 4;
        public const int s_walk_stone_l           = 5;
        public const int s_walk_stone_r           = 6;
        public const int s_run_stone_l            = 7;
        public const int s_run_stone_r            = 8;
        public const int s_walk_lawn_l            = 9;
        public const int s_walk_lawn_r            = 10;
        public const int s_run_lawn_l             = 11;
        public const int s_run_lawn_r             = 12;
        public const int s_walk_rough_l           = 13;
        public const int s_walk_rough_r           = 14;
        public const int s_run_rough_l            = 15;
        public const int s_run_rough_r            = 16;
        public const int s_walk_wood_l            = 17;
        public const int s_walk_wood_r            = 18;
        public const int s_run_wood_l             = 19;
        public const int s_run_wood_r             = 20;
        public const int s_walk_cave_l            = 21;
        public const int s_walk_cave_r            = 22;
        public const int s_run_cave_l             = 23;
        public const int s_run_cave_r             = 24;
        public const int s_walk_room_l            = 25;
        public const int s_walk_room_r            = 26;
        public const int s_run_room_l             = 27;
        public const int s_run_room_r             = 28;
        public const int s_walk_water_l           = 29;
        public const int s_walk_water_r           = 30;
        public const int s_run_water_l            = 31;
        public const int s_run_water_r            = 32;

        public const int s_hit_short              = 50;
        public const int s_hit_wooden             = 51;
        public const int s_hit_sword              = 52;
        public const int s_hit_do                 = 53;
        public const int s_hit_axe                = 54;
        public const int s_hit_club               = 55;
        public const int s_hit_long               = 56;
        public const int s_hit_fist               = 57;

        public const int s_struck_short           = 60;
        public const int s_struck_wooden          = 61;
        public const int s_struck_sword           = 62;
        public const int s_struck_do              = 63;
        public const int s_struck_axe             = 64;
        public const int s_struck_club            = 65;

        public const int s_struck_body_sword      = 70;
        public const int s_struck_body_axe        = 71;
        public const int s_struck_body_longstick  = 72;
        public const int s_struck_body_fist       = 73;

        public const int s_struck_armor_sword     = 80;
        public const int s_struck_armor_axe       = 81;
        public const int s_struck_armor_longstick = 82;
        public const int s_struck_armor_fist      = 83;

        public const int s_strike_stone           = 91;
        public const int s_drop_stonepiece        = 92;

        public const int s_rock_door_open         = 100;
        public const int s_intro_theme            = 102;
        public const int s_meltstone              = 101;
        public const int s_main_theme             = 102;
        public const int s_norm_button_click      = 103;
        public const int s_rock_button_click      = 104;
        public const int s_glass_button_click     = 105;
        public const int s_money                  = 106;
        public const int s_eat_drug               = 107;
        public const int s_click_drug             = 108;
        public const int s_spacemove_out          = 109;
        public const int s_spacemove_in           = 110;

        public const int s_click_weapon           = 111;
        public const int s_click_armor            = 112;
        public const int s_click_ring             = 113;
        public const int s_click_armring          = 114;
        public const int s_click_necklace         = 115;
        public const int s_click_helmet           = 116;
        public const int s_click_grobes           = 117;
        public const int s_itmclick               = 118;

        public const int s_deal_additem           = 125;
        public const int s_deal_delitem           = 126;

        public const int s_yedo_man               = 130;
        public const int s_yedo_woman             = 131;
        public const int s_longhit                = 132;
        public const int s_widehit                = 133;

        public const int s_rush_l                 = 134;
        public const int s_rush_r                 = 135;
        public const int s_firehit_ready          = 136;
        public const int s_firehit                = 137;
        public const int s_crshit                 = 140;
        public const int s_twinhit                = 141;
        public const int s_baldo                  = 141;

        public const int s_man_struck             = 138;
        public const int s_wom_struck             = 139;
        public const int s_man_die                = 144;
        public const int s_wom_die                = 145;

        public static void LoadSoundList(string flname)
        {
            int k, idx, n;
            string[] strlist;
            string data1, data2;

            if (File.Exists(flname) == false)
                return;

            try
            {
                strlist = File.ReadAllLines(flname);
                idx = 0;

                foreach (string str in strlist)
                {
                    if (string.IsNullOrEmpty(str)) continue;
                    if (str[0] == ';') continue;

                    // data1 = index, data2 = filename
                    if (ClientGlobals.PickString(str, out data1, 0, ':', ' ', '\t') == false) continue;
                    if (ClientGlobals.PickString(str, out data2, 1, ':', ' ', '\t') == false) continue;

                    data2 = data2.Trim();
                    n = System.Convert.ToInt32(data1);
                    if (n > idx)
                    {
                        for (k = 0; k < (n - (ClientGlobals.SoundList.Count - 1)); k++)
                        {
                            ClientGlobals.SoundList.Add("");
                        }
                        ClientGlobals.SoundList.Add(str);
                        idx = n;
                    }
                }
            }
            catch
            {
            }
        }

        public static void PlaySound(int idx)
        {
            if (ClientGlobals.Sound != null && ClientGlobals.BoPlaySoundEffect)
            {
                if (idx > 0 && idx < ClientGlobals.SoundList.Count)
                {
                    if (ClientGlobals.SoundList[idx] != "") 
                    {
                        if (File.Exists(ClientGlobals.SoundList[idx]))
                        {
                            try
                            {
                                ClientGlobals.Sound.SoundLocation = ClientGlobals.SoundList[idx];
                                ClientGlobals.Sound.LoadAsync();
                                ClientGlobals.Sound.Play();
                            }
                            catch (Exception)
                            {
                                // Dont Throw
                            }
                        }
                    }
                }
            }
        }

        public static void SilenceSound()
        {
            if (ClientGlobals.Sound != null)
            {
                ClientGlobals.Sound.Stop();
            }
        }

        public static void ItemUseSound(int stdmode)
        {
            switch (stdmode)
            {
                case 0: PlaySound(s_click_drug);
                goto case 1;
                case 1:
                case 2:
                    PlaySound (s_eat_drug);
                goto default;
                default: break;
            }
        }


    }
}
